/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.extend.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.star.framework.extend.core.Database;
import cn.star.framework.extend.core.DatabaseFactory;
import cn.star.framework.extend.provider.SpringConnectionProvider;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AutoCfg
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Configuration
public class AutoCfg {

  @Bean(name = "__F.AUTO_DEFAULT_DATABASE__")
  public Database database(
      final DataSourceProperties dataSourceProperties,
      final JpaProperties jpaProperties,
      final DataSource dataSource) {
    Cfg cfg = new Cfg();

    cfg.setUrl(dataSourceProperties.getUrl());
    cfg.setUsername(dataSourceProperties.getUsername());
    cfg.setPassword(dataSourceProperties.getPassword());
    cfg.setDriver(dataSourceProperties.getDriverClassName());

    Map<String, String> properties = jpaProperties.getProperties();
    if (CollectionUtil.isNotEmpty(properties)) {
      properties.forEach(cfg::addProperty);
    }

    // 解决方言不一致的问题
    String dialect = jpaProperties.getDatabasePlatform();
    Object value = cfg.getProperty(AvailableSettings.DIALECT);
    if (ObjectUtil.isNotEmpty(value) && value instanceof String) {
      dialect = (String) value;
    }
    cfg.setDialect(dialect);

    cfg.addProperty(AvailableSettings.CONNECTION_PROVIDER, SpringConnectionProvider.class)
        .setDataSource(dataSource);

    // Cfg cfg = new Cfg();
    // cfg.setUrl("jdbc:h2:tcp://localhost:9092/./test");
    // cfg.setUsername("sa");
    // cfg.setPassword("");
    // cfg.setDriver(SupportType.H2.getDriver());
    // cfg.setDialect(SupportType.H2.getDialect());
    // cfg.setDialect("com.intronlink.framework.extend.dialect.H2Dialect");
    // cfg.setSchema("PUBLIC");

    return DatabaseFactory.get(cfg);
  }
}
